package service

import (
	"fmt"
	"github.com/vueadmin/app/admin/internal/dto"
	"github.com/vueadmin/app/admin/internal/model"
	"github.com/vueadmin/app/admin/internal/svc"
	"github.com/vueadmin/app/admin/internal/vo"
	"github.com/vueadmin/utils/conv"
)

type linkcataService struct{
	svcCtx *svc.ServiceContext
}

func NewLinkcataService(svcCtx *svc.ServiceContext) *linkcataService {
	return &linkcataService{
		svcCtx: svcCtx,
	}
}

/**
 * @description(数据列表)
 * @buildcode(true)
*/
func (p linkcataService) GetPageList(req *dto.LinkcataPageReq) ([]*vo.LinkcataList, int64, error) {
	var (
		list  []*vo.LinkcataList
		count int64
	)
	where := "1=1 "
	if !conv.IsEmpty(req.LinkcataId) {
		where += fmt.Sprintf(`and a.linkcata_id = %d `, *req.LinkcataId)
	}
	if !conv.IsEmpty(req.ClassName) {
		where += fmt.Sprintf(`and a.class_name = "%s" `, req.ClassName)
	}
	if !conv.IsEmpty(req.Status) {
		where += fmt.Sprintf(`and a.status = %d `, *req.Status)
	}
	if !conv.IsEmpty(req.Count) {
		where += fmt.Sprintf(`and a.count = "%s" `, req.Count)
	}
	order := "linkcata_id desc"
	if !conv.IsEmpty(req.Sort) && !conv.IsEmpty(req.Order) {
		order = fmt.Sprintf("%s %s", req.Sort, req.Order)
	}
	skip := (req.Page - 1) * req.Limit
	sql := fmt.Sprintf(`select a.*,count(*) as count from cd_linkcata as a left join cd_link as b on a.linkcata_id = b.linkcata_id  where %s group by a.linkcata_id  order by %s limit %d,%d`, where,order,skip, req.Limit)
	if err := p.svcCtx.Db.Raw(sql).Scan(&list).Error; err != nil {
		return nil,0,err
	}
	countSql := fmt.Sprintf(`select count(*) as count from (select a.*,count(*) as count from cd_linkcata as a left join cd_link as b on a.linkcata_id = b.linkcata_id  where %s group by a.linkcata_id ) as tmp`, where)
	p.svcCtx.Db.Raw(countSql).Scan(&count)

	return list, count, nil
}

/**
 * @description(修改排序开关)
 * @buildcode(true)
*/
func (p linkcataService) UpdateExt(req map[string]interface{}) error {
	entity := model.Linkcata{}
	if err := p.svcCtx.Db.Model(&entity).Where("linkcata_id",conv.Int(req["linkcata_id"])).Updates(req).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(添加)
 * @buildcode(true)
*/
func (p linkcataService) Add(req *dto.LinkcataAddReq) (int, error) {
	entity := model.Linkcata{}
	entity.ClassName = req.ClassName
	entity.Status = req.Status
	entity.Jdt = req.Jdt

	if err := p.svcCtx.Db.Create(&entity).Error; err != nil {
		return 0, err
	}
	return entity.LinkcataId, nil
}

/**
 * @description(修改)
 * @buildcode(true)
*/
func (p linkcataService) Update(req *dto.LinkcataUpdateReq) error {
	entity := model.Linkcata{
		LinkcataId: req.LinkcataId,
	}
	query := p.svcCtx.Db
	if err := query.Limit(1).First(&entity).Error; err != nil {
		return err
	}
	entity.ClassName = req.ClassName
	entity.Status = req.Status
	entity.Jdt = req.Jdt

	field := conv.StringToSlice("class_name,status,jdt", ",")

	if err := query.Select(field).Updates(&entity).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(获取修改详情)
 * @buildcode(true)
*/
func (p linkcataService) GetUpdateInfo(req *dto.LinkcataGetUpdateInfoReq) (*model.Linkcata, error) {
	entity := model.Linkcata{}
	if err := p.svcCtx.Db.Where("linkcata_id = ?",req.LinkcataId).Limit(1).First(&entity).Error; err != nil {
		return nil, err
	}
	return &entity, nil
}

/**
 * @description(删除)
 * @buildcode(true)
*/
func (p linkcataService) Delete(req *dto.LinkcataDeleteReq) error {
	query := p.svcCtx.Db
	query = query.Where("linkcata_id IN ?", req.LinkcataId)
	if err := query.Delete(&model.Linkcata{}).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(查看详情)
 * @buildcode(true)
*/
func (p linkcataService) Detail(req *dto.LinkcataDetailReq) (*vo.LinkcataDetail, error) {
	var info vo.LinkcataDetail
	query := p.svcCtx.Db.Model(&model.Linkcata{})
	if err := query.Where("linkcata_id = ?",req.LinkcataId).Limit(1).First(&info).Error; err != nil {
		return nil, err
	}
	return &info, nil
}

